<template>
  <div class="login">
    <div class="login-form">
      <div class="logo"></div>
      <el-form
        ref="loginForm"
        class="form"
        :model="loginForm"
        :rules="ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="shopicon icon-yonghu"
            v-model="loginForm.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="shopicon icon-mima"
            placeholder="请输入密码"
            type="password"
            v-model="loginForm.password"
          />
        </el-form-item>
        <el-form-item class="btns">
          <el-button
            type="primary"
            @click="handleLogin"
          >
            登录
          </el-button>
          <el-button type="info">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { log } from "util";
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      ruleForm: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 5,
            message: "3-5位",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 10,
            message: "6-10位",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          const response = await this.$http.post("login", this.loginForm);

          const { data: res } = response;
          // res === response.data;

          if (res.meta.status === 200) {
            window.localStorage.setItem("token", res.data.token);
            // this.$message.success("登录成功", 2000, () => {
            //   this.$router.push("/home");
            // });
            this.$message({
              type: "success",
              message: "登录成功",
              duration: 1000,
              onClose: () => {
                this.$router.push("/home");
              },
            });
          } else {
            this.$message.error("登录失败");
          }
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.login {
  height: 100%;
  background: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    width: 450px;
    height: 300px;
    background: #fff;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
    position: relative;
    .logo {
      width: 120px;
      height: 120px;
      padding: 10px;
      border-radius: 50%;
      background: #fff;
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
      &:after {
        content: "";
        position: absolute;
        left: 10px;
        top: 10px;
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background: #eee url("../../assets/logo.png") 0 0 no-repeat;
        background-size: 100%;
      }
    }
    .form {
      padding: 104px 32px 0;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>